﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_SetUp.Positions
{
    public class TMS_PositionRepositon: DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_PositionRepositon
    {
        public TMS_PositionRepositon(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="tMS_Department"></param>
        /// <returns></returns>
        public async Task<int> CreatePosition(TMS_Position tMS_Position)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"insert into TMS_Position values(NEWID(),'{tMS_Position.Position_Name}','{tMS_Position.Position_Alias}','{tMS_Position.Department_Id}',1,GETDATE())", null,
                 await GetDbTransactionAsync());
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task<int> DeletePosition(string id)
        {
            var dbConnection = await GetDbConnectionAsync();
            var count = 0;
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                count += await dbConnection.ExecuteAsync($"update TMS_Position set Position_Delete = 'false' where Id = '{item}'", null,
                 await GetDbTransactionAsync());
            }
            return count;
        }
        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>

        public async Task<PageResponse<TMS_PositionDto>> GetPosition(string name, int pageindex=1, int pagesize=5)
        {
            PageResponse<TMS_PositionDto> response = new PageResponse<TMS_PositionDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select a.*,b.Department_Name from TMS_Position a join TMS_Department b on a.Department_Id = b.Id where a.Position_Delete = 'true'";
            var list = await dbConnection.QueryAsync<TMS_PositionDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageTotal = list.Count();
            if (name != null)
            {
                sql += $" and a.Position_Name like '%{name}%'";
            }
            sql += $" order by a.Position_DateTime offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await dbConnection.QueryAsync<TMS_PositionDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageData = lists.ToList();
            return response;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tMS_DepartmentDto"></param>
        /// <returns></returns>

        public async Task<int> UpdatePosition(TMS_PositionDto tMS_Position)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_Position set Position_Name = '{tMS_Position.Position_Name}',Department_Id='{tMS_Position.Department_Id}',Position_Alias='{tMS_Position.Position_Alias}' where Id = '{tMS_Position.Id}'", null,
                 await GetDbTransactionAsync());
        }
    }
}
